//
//  MeController.m
//  LingQian
//
//  Created by xieyingze on 2018/1/21.
//  Copyright © 2018年 xieyingze. All rights reserved.
//

#import "MeController.h"
#import "MeCell.h"
#import "MeHeaderView.h"
#import "MeTableViewCell.h"
#import "MeViewModel.h"

@interface MeController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) MeHeaderView *headerView;

@property (nonatomic, strong) MeViewModel *viewModel;

@end

@implementation MeController

#pragma mark - ********************生命周期********************

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - ********************配置信息********************

- (void)configuration {
    self.navigationController.navigationBar.hidden = YES;
    self.navTitle = @"我的";
    
}

- (void)initSubViews {
    [self.view addSubview:self.tableView];
}

- (void)autoLayoutForSubViews {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0).insets(UIEdgeInsetsMake(-20, 0, 0, 0));
    }];
}

- (void)bindViewModel {
    @weakify(self);
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:kNotifi_meRefresh object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self);
        [self.tableView reloadData];
    }];
}


#pragma mark - ********************基础方法********************


#pragma mark - ********************代理方法********************

#pragma mark - UITableViewDataSource
//返回组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section == 0 ? 1 : 1;
}

//返回Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        MeCell *cell = [MeCell cellWithTableView:tableView indexPath:indexPath];
        cell.viewModel = self.viewModel;
        return cell;
    } else {
        MeTableViewCell *cell = [MeTableViewCell cellWithTableView:tableView indexPath:indexPath];
        cell.indexPath = indexPath;
        cell.viewModel = self.viewModel;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return section == 0 ? 100 : 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.section == 0 ? 120 : kUI_CellHeight * 3 + 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return section == 0 ? self.headerView : [UIView new];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.headerView.offset = scrollView.contentOffset.y;
}

#pragma mark - ********************响应事件********************



#pragma mark - ********************懒加载*********************

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.backgroundColor = kCOLOR_tableView;
        _tableView.rowHeight = 110;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

- (MeHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [MeHeaderView new];
    }
    return _headerView;
}

- (MeViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [MeViewModel new];
    }
    return _viewModel;
}

@end
